﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using PSTS.Models;
using PSTS.UI.Commands;
using PSTS.UI.Views;
using System.Threading;

namespace PSTS.UI.ViewModels
{
    public class AdminPageViewModel:ViewModelBase
    {
        private bool mDelChapterEnabled = false;
        public bool DelChapterEnabled
        {
            get { return mDelChapterEnabled ; }
            set
            {
                if (mDelChapterEnabled != value)
                {
                    mDelChapterEnabled = value;
                    OnPropertyChanged("DelChapterEnabled");
                }
            }
        }

        private bool mDelTopicEnabled = false;
        public bool DelTopicEnabled
        {
            get { return mDelTopicEnabled ; }
            set
            {
                if (mDelTopicEnabled != value)
                {
                    mDelTopicEnabled = value;
                    OnPropertyChanged("DelTopicEnabled");
                }
            }
        }

        private bool mDelQuestionEnabled = false;
        public bool DelQuestionEnabled
        {
            get { return mDelQuestionEnabled; }
            set
            {
                if (mDelQuestionEnabled != value)
                {
                    mDelQuestionEnabled = value;
                    OnPropertyChanged("DelQuestionEnabled");
                }
            }
        }

        private bool mShow = false;
        public bool Show
        {
            get { return mShow; }
            set
            {
                if (mShow != value)
                {
                    mShow = value;
                }
                OnPropertyChanged("Show");
            }
        }

        public AdminPageViewModel()
        {
            mChapterList = Repository.Instance.GetAllChaptersList();
        }

        private string mErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return mErrorMessage; }
            set
            {
                if (mErrorMessage != value)
                {
                    mErrorMessage = value;
                    OnPropertyChanged("ErrorMessage");
                }
            }
        }

        private List<Chapter> mChapterList = new List<Chapter>();

        public List<Chapter> ChapterList
        {
            get { return mChapterList; }
            set
            {
                if (mChapterList != value)
                {
                    mChapterList = value;
                }
                
                OnPropertyChanged("ChapterList");
                
            }
        }

        private List<Topic> mTopicList = new List<Topic>();

        public List<Topic> TopicList
        {
            get { return mTopicList; }
            set
            {
                if (mTopicList != value)
                {
                    mTopicList = value;
                }
                OnPropertyChanged("TopicList");
            }
        }

        private List<Question> mQuestionList = new List<Question>();

        public List<Question> QuestionList
        {
            get { return mQuestionList; }
            set
            {
                if (mQuestionList != value)
                {
                    mQuestionList = value;
                }
                OnPropertyChanged("QuestionList");
            }
        }

        private Chapter mSelectedChapter;
        public Chapter SelectedChapter
        {
            get { return mSelectedChapter; }
            set
            {
                if (mSelectedChapter != value)
                {
                    mSelectedChapter = value;
                    DelChapterEnabled = true;
                    TopicList = Repository.Instance.GetChapterTopicsList(mSelectedChapter.Id);
                    OnPropertyChanged("SelectedChapter");
                    OnPropertyChanged("TopicList");
                }
            }
        }

        private Topic mSelectedTopic;
        public Topic SelectedTopic
        {
            get { return mSelectedTopic; }
            set
            {
                if (mSelectedTopic != value)
                {
                    mSelectedTopic = value;
                    DelTopicEnabled = true;
                    QuestionList = Repository.Instance.GetAllTopicsQuestionsList(mSelectedTopic.Id);
                    OnPropertyChanged("SelectedTopic");
                    OnPropertyChanged("QuestionList");
                }
            }
        }

        private Question mSelectedQuestion;
        public Question SelectedQuestion
        {
            get { return mSelectedQuestion; }
            set
            {
                if (mSelectedQuestion != value)
                {
                    mSelectedQuestion = value;
                    DelQuestionEnabled = true;
                    Answers = new ObservableCollection<Answer>(mSelectedQuestion.GetListOfAnswers());
                    ErrorMessage = string.Empty;
                    OnPropertyChanged("SelectedQuestion");
                }
            }
        }

        private ObservableCollection<Answer> mAnswers = new ObservableCollection<Answer>();
        public ObservableCollection<Answer> Answers
        {
            get { return mAnswers; }
            set
            {
                if (mAnswers != value)
                {
                    mAnswers = value;
                    OnPropertyChanged("Answers");
                }
            }
        }

        private DelegateCommand mSave;
        public ICommand Save
        {
            get
            {
                if(mSave == null)
                    mSave = new DelegateCommand(delegate { SaveChanges(); });
                return mSave;
            }
        }


        private void SaveChanges()
        {        
            bool IsChecked = false;
            var index = -1;
            if (SelectedQuestion == null)
            {
                ErrorMessage = "Оберіть запитання, будь ласка.";
                return;
            }
            SelectedQuestion.Answers = new List<string>();
            foreach (var answer in Answers)
            {
                if (string.IsNullOrEmpty(answer.Value))
                {
                    ErrorMessage = "Відповідь повинна містити текст.";
                    return;
                }
                if (answer.IsCorrect && IsChecked)
                {
                    index = -1;
                    ErrorMessage = "Ви обрали надто багато варіантів відповіді, оберіть один.";
                    return;
                }
                SelectedQuestion.Answers.Add(answer.Value);
                if (answer.IsCorrect)
                {
                    index = SelectedQuestion.Answers.IndexOf(answer.Value);
                    IsChecked = true;
                }          
            }
            if (string.IsNullOrEmpty(SelectedQuestion.Text))
            {
                ErrorMessage = "Запитання повинне містити текст.";
                return;
            }
            if (index == -1)
            {
                ErrorMessage = "Вкажіть правильну відповідь, будь ласка.";
                return;
            }
            SelectedQuestion.CorrectAnsIndex = index;
            Repository.Instance.UpdateQuestion(SelectedQuestion);
            Repository.Instance.UpdateTopic(SelectedTopic);
            Repository.Instance.UpdateChapter(SelectedChapter);
            ErrorMessage = string.Empty;
            OnPropertyChanged("SelectedQuestion");
        }

        private string mQuestionText = string.Empty;
        public string QuestionText
        {
            get { return mQuestionText; }
            set
            {
                if (mQuestionText != value)
                {
                    mQuestionText = value;
                    OnPropertyChanged("QuestionText");
                }
            }
        }

        private DelegateCommand mGoToFolder;

        public ICommand GoToFolder
        {
            get
            {
                if(mGoToFolder == null)
                    mGoToFolder = new DelegateCommand(delegate { GoToTheFolder(); });
                return mGoToFolder;
            }
        }

        private void GoToTheFolder()
        {
            if (string.IsNullOrEmpty(ImportedChapterName))
            {
                ErrorMessage = "Вкажіть назву розділу, будь ласка.";
                return;
            }
            ErrorMessage = string.Empty;
            var folderDialog = new OpenFileDialog();

            DialogResult result = folderDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                CurrentPath = folderDialog.FileName;
            }
        }

        private string mCurrentPath;
        public string CurrentPath
        {
            get
            {
                return mCurrentPath;
            }
            set
            {
                if (value != mCurrentPath)
                {
                    mCurrentPath = value;
                    ImportChapter();
                    OnPropertyChanged("CurrentPath");
                }
            }
        }

        private string mImportedChapterName;
        public string ImportedChapterName
        {
            get
            {
                return mImportedChapterName;
            }
            set
            {
                if (value != mImportedChapterName)
                {
                    mImportedChapterName = value;
                    OnPropertyChanged("ImportedChapterName");
                }
            }
        }

        private void LoadChapter()
        {
            //Thread.Sleep(5000);
            try
            {
                if (!Repository.Instance.HasChapter(ImportedChapterName))
                {
                    Repository.Instance.AddChapter(ImportedChapterName);
                }
                var chapter = Repository.Instance.GetChapter(ImportedChapterName);
                Converter.Instance.AddTopicsToChapter(chapter.Id, CurrentPath);
                ChapterList = Repository.Instance.GetAllChaptersList();
                ImportedChapterName = "";
            }
            catch (Exception e)
            {
                ErrorMessage = "Помилка при завантаженні курсу";
            }
            finally
            {
                Show = false;
            }
            
        }

        private void ImportChapter()
        {
            var calcThread = new Thread(LoadChapter);
            calcThread.Start();
            Show = true;       
            //ImportedChapterName = "";
        }

        private DelegateCommand mDeleteChapter;
        public ICommand DeleteChapter
        {
            get
            {
                if(mDeleteChapter == null)
                    mDeleteChapter = new DelegateCommand(o => DeleteTheChapter());
                return mDeleteChapter;
            }
        }

        private void DeleteTheChapter()
        {
            QuestionList = Repository.Instance.GetAllTopicsQuestionsList(SelectedTopic);
            SelectedQuestion = new Question();
            Answers = new ObservableCollection<Answer>((new Question()).GetListOfAnswers());

            TopicList = Repository.Instance.GetAllTopicsList();

            Repository.Instance.DeleteChapter(SelectedChapter);
            ChapterList = Repository.Instance.GetAllChaptersList();

            DelChapterEnabled = false;
            DelTopicEnabled = false;
            DelQuestionEnabled = false;
        }

        private DelegateCommand mDeleteTopic;
        public ICommand DeleteTopic
        {
            get
            {
                if (mDeleteTopic == null)
                {
                    mDeleteTopic = new DelegateCommand(o => DeleteTheTopic());
                }
                return mDeleteTopic;
            }
        }

        private void DeleteTheTopic()
        {
            QuestionList = Repository.Instance.GetAllTopicsQuestionsList(SelectedTopic);
            SelectedQuestion = new Question();
            Answers = new ObservableCollection<Answer>((new Question()).GetListOfAnswers());

            Repository.Instance.DeleteTopic(SelectedTopic);
            TopicList = Repository.Instance.GetAllTopicsList();

            DelTopicEnabled = false;
            DelQuestionEnabled = false;
            return;
        }

        private DelegateCommand mDeleteQuestion;
        public ICommand DeleteQuestion
        {
            get
            {
                if (mDeleteQuestion == null)
                {
                    mDeleteQuestion = new DelegateCommand(o => DeleteTheQuestion());
                }
                return mDeleteQuestion;
            }
        }

        private void DeleteTheQuestion()
        {
            Repository.Instance.DeleteQuestion(SelectedQuestion);
            QuestionList = Repository.Instance.GetAllTopicsQuestionsList(SelectedTopic);
            SelectedQuestion = new Question();
            Answers = new ObservableCollection<Answer>((new Question()).GetListOfAnswers());
            DelQuestionEnabled = false;
        }
    }
}
